package ui.activity.mine.presenter;


import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import Bean.MessagecenterBean;
import Bean.MessagecenterParamBean;
import Bean.MessagecenterReadParamBean;
import base.BaseBiz;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import ui.activity.mine.biz.MessageCenterBiz;
import ui.activity.mine.contract.MessageCenterContract;

/**
 * Created by chenjian on 19/2/26
 */

public class MessageCenterPresenter implements MessageCenterContract.Presenter {

    MessageCenterContract.View view;
    MessageCenterBiz messageCenterBiz;
    List<MessagecenterBean.DataBean.ItemsBean> list = new ArrayList<>();


    @Inject
    public MessageCenterPresenter(MessageCenterContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        messageCenterBiz = (MessageCenterBiz) biz;
    }


    @Override
    public void getMessageList(boolean isRefresh, String id, int type) {
        if (isRefresh) {
            list.clear();
        }
        view.loading(true);
        MessagecenterParamBean m = new MessagecenterParamBean();
        m.setCustomId(id);
        m.setType(type + "");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(m));
        messageCenterBiz.getMessageList(requestBody, new BaseBiz.Callback<MessagecenterBean.DataBean>() {
            @Override
            public void onSuccess(MessagecenterBean.DataBean info) {
                list.addAll(info.getItems());
                view.upDateUI(list, info.getReadnum());
                view.hasLoadMore(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
                view.loading(false);
                setMessageType(id, type);
            }

            @Override
            public void onFailure(MessagecenterBean.DataBean info) {
                view.loading(false);
                if (isRefresh) {
                    view.refreshComplete();
                } else {
                    view.loadMoreComplete();
                }
            }
        });

    }

    @Override
    public void setMessageType(String id, int type) {
        MessagecenterReadParamBean m = new MessagecenterReadParamBean();
        m.setCustomId(id);
        m.setType(type + "");
        m.setIsRead("1");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(m));
        messageCenterBiz.getMessageList(requestBody, new BaseBiz.Callback<MessagecenterBean.DataBean>() {
            @Override
            public void onSuccess(MessagecenterBean.DataBean info) {
            }

            @Override
            public void onFailure(MessagecenterBean.DataBean info) {
            }
        });

    }

    @Override
    public void setNumberType(String id, int type) {
        MessagecenterParamBean m = new MessagecenterParamBean();
        m.setCustomId(id);
        m.setType(type + "");
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), new Gson().toJson(m));
        messageCenterBiz.getMessageList(requestBody, new BaseBiz.Callback<MessagecenterBean.DataBean>() {
            @Override
            public void onSuccess(MessagecenterBean.DataBean info) {
                view.upDateNumber(type, info.getReadnum());
            }

            @Override
            public void onFailure(MessagecenterBean.DataBean info) {
            }
        });
    }

}
